clear all;
clc;

% ------------------------- File Information ------------------------------

numpat = 5;


epitag = 'epi_bp_current_';
subtag = 'sub_bp_current_';

epi_f  = [0.3,0.45,0.23,0.21,0.25];
sub1_f = [0.2,0.12,0.25,0.3,0.16];
sub2_f = [0.8,0.88,0.75,0.7,0.84];

th_val = [0,-10,-20];
noth = length(th_val);

% ------------------------- Importing Data --------------------------------

i_epi  = zeros(noth,numpat);
i_sub1 = zeros(noth,numpat);
i_sub2 = zeros(noth,numpat);

for i = 1:numpat
    
    ptag=['p',num2str(i),'_'];
    
    for j = 1:length(th_val)
        
        cd(['patient',num2str(i),'_potentials']);
        
        epitemp = [ptag,epitag,'d',num2str(epi_f(i)),...
                    '_t',num2str(th_val(j))];
        sub1temp = [ptag,subtag,'d',num2str(sub1_f(i)),...
                    '_t',num2str(th_val(j))];
        sub2temp = [ptag,subtag,'d',num2str(sub2_f(i)),...
                    '_t',num2str(th_val(j))];
               
        epifile = strrep(epitemp,'.','p');
        epifile = strrep(epifile,'-','n');
        sub1file = strrep(sub1temp,'.','p');
        sub1file = strrep(sub1file,'-','n');
        sub2file = strrep(sub2temp,'.','p');
        sub2file = strrep(sub2file,'-','n');
        
        i_epi(j,i) = load([epifile,'.txt']);
        i_sub1(j,i) = load([sub1file,'.txt']);
        i_sub2(j,i) = load([sub2file,'.txt']);
        
        cd ..;
        
    end
end

% ------------------------- Analysis R_Access -----------------------------

mu_repi  = mean(2./i_epi,2);
sd_repi  = std(2./i_epi,0,2);
mu_rsub1 = mean(2./i_sub1,2);
sd_rsub1 = std(2./i_sub1,0,2);
mu_rsub2 = mean(2./i_sub2,2);
sd_rsub2 = std(2./i_sub2,0,2);

mu_all = cat(1,mu_repi',mu_rsub2',mu_rsub1');
sd_all = cat(1,sd_repi',sd_rsub2',sd_rsub1'); 

% barweb(mu_all,sd_all/sqrt(numpat));
barweb(mu_all,sd_all);
colormap('gray');
X1 = '1 mm above dura';
X2 = '1 mm below dura';
X3 = '1 mm above cord';
L1 = '0^{\circ}';
L2 = '-10^{\circ}';
L3 = '-20^{\circ}';
% ttl = 'Bipolar Stimulation with AD-Tech Array (n = 5 Patients)';
% title(ttl,'FontSize',30,'FontWeight','b');
ylabel('Access Resistance (\Omega)','FontSize',36);
legend(L1,L2,L3,'location','NE');
set(gca,'FontSize',36);
set(gca,'XTickLabel',{X1,X2,X3},'YTick',250:250:1250);
% xlim([0.65,1.35]);
% axis square;